﻿using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;

namespace AgencyManagerSystem.Core
{
    public class RoleUserUpdaterCreator : BaseUpdaterCreator<RoleUserModel>
    {
        public RoleUserUpdater CreateRoleUserUpdaterInstance()
        {
            RoleUserUpdater roleUserUpdater = new RoleUserUpdater(new RoleUserFetcher());
            roleUserUpdater.DataUpdating += FlagAndApproveFlagValid;
            roleUserUpdater.DataUpdating += ReferenceDocExists;
            return roleUserUpdater;
        }

        private void ReferenceDocExists(BaseUpdater<RoleUserModel> sender, DataUpdatingEventArgs<RoleUserModel> e)
        {
            if (e.Insert == null)
                return;
            DomainFetcher domainFetcher = new DomainFetcher();
            UserFetcher userFetcher = new UserFetcher();
            RoleFetcher roleFetcher = new RoleFetcher();
            DomainModel domainModel = domainFetcher.Get(e.Insert.Domain);
            UserModel userModel = userFetcher.Get(e.Insert.User);
            RoleModel roleModel = roleFetcher.Get(e.Insert.Role);
            if(domainModel==null||userModel==null||roleModel ==null)
            {
                e.Message = "参考档案不存在";
                e.Cancel = true;
                return;
            }
        }
    }
}
